package com.fbank.demo.config;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

@Configuration
@MapperScan(basePackages = "com.fbank.demo.dao")
public class MybatisConfigure {

    @Bean
    public DataSource getDataSource() throws Exception {
        Properties properties = new Properties();
        properties.put("driveClassName", "com.mysql.cj.jdbc.Driver");
        properties.put("url", "jdbc:mysql://10.1.52.201:3306/inte-lt?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
        properties.put("username", "root");
        properties.put("password", "AtaV60sc");
        return DruidDataSourceFactory.createDataSource(properties);

    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory()  throws Exception {
        try {
            SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
            sessionFactoryBean.setDataSource(getDataSource());
            Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/*.xml");
            sessionFactoryBean.setMapperLocations(resources);
            return sessionFactoryBean.getObject();
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw e;
        }
    }

}
